<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_loadExtraHeaderAction extends f_core_Action
{
	public function execute()
	{
        $data = $this->getRequestParameter("f");
        
        if (empty($data))
        {
            header('HTTP/1.0 404 Not Found');
            echo "SYNTAX: index.php/x.js?f=dir1/file1.js,dir2/file2.js";
            exit;
        }

        $contentType = false;

        if($this->getRequestParameter("c"))
        {
            $contentType = $this->getRequestParameter("c") === 'auto' ? true : $this->getRequestParameter("c");
        }

        $files = is_array($data) ? $data : explode(',', $data);

        $contentTypeAllowed = array('text/javascript', 'text/css');
        $fileExtAllowed = array('js', 'css');

        $out = '';
        $lastModified = 0;

        foreach ($files as $f)
        {
            $fileRealPath = HTTP_DIR.DS.$f;
            $file = f_file_File::getInstance($fileRealPath);

            $fileExt = $file->getExtension();

            if (empty($fileExt) || !in_array($fileExt, $fileExtAllowed))
            {
                continue;
            }

            if ($contentType === true)
            {
                $contentTypes = array('js' => 'text/javascript','css' => 'text/css');

                if(empty($contentTypes[$fileExt]))
                {
                    continue;
                }

                $contentType = !empty($contentTypes[$fileExt]) ? $contentTypes[$fileExt] : false;
            }

            $out .= file_get_contents($fileRealPath) . "\n";
            $lastModified = max($lastModified, filemtime($fileRealPath));
        }

        if (function_exists('date_default_timezone_set'))
        {
            date_default_timezone_set('UTC');
        }

        if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified)
        {
            header("HTTP/1.1 304 Not Modified");
            exit;
        }

        header('Cache-Control: must-revalidate');
        header('Last-modified: ' . gmdate('r', $lastModified));

        if (is_string($contentType) && in_array($contentType, $contentTypeAllowed))
        {
            header('Content-type: '.$contentType);
        }

        if ($this->getRequestParameter("s"))
        {
            $out = preg_replace('#[ \t]+#', ' ', $out);
        }

        if($this->getRequestParameter("e"))
        {
            $time = time()+ 365 * 86400;
            header('Expires: '.gmdate('r', $time));
        }

        echo $out;
	}	
	
	public function validate()
	{
		return true;
	}
}